package m.jclecai.com.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.widget.EditText;

import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import m.jclecai.com.R;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.bean.ProofBean;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.utils.MyToast;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;

public class NewlyResetPassWordActivity extends BaseActivity {

    @BindView(R.id.et_phone)
    EditText et_phone;

    @Override
    public int getContentView() {
        return R.layout.activity_newly;
    }


    @Override
    public void initView() {
        leftOptionText("");
        rightOptionText("忘记用户名");
        setTitle("忘记密码");
    }

    @OnClick(R.id.tv_right_option)
    void forgetUserName(){
        startActivity(new Intent(this,ResetPassWordActivity.class)
                .putExtra("title","忘记用户名、密码"));
        finish();
    }

    @OnClick(R.id.text_bottom)
    void next(){
        String et_name = et_phone.getText().toString().trim();
        if(!TextUtils.isEmpty(et_name)){
            isUserName(et_name);
        }else {
            MyToast.showToast(this,"用户名不能为空!");
        }
    }

    /**
     * 判断是否存在此用户
     */
    private void isUserName(final String et_name) {
        showDialog();
        HashMap<String, String> pair = new HashMap<>();
        pair.put("user",et_name);
        HttpManager.postAsyn(Constant_Url.CHECK_USER_NAME_URL, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                dissDialog();
                DisPlay(getString(R.string.error_network));
            }

            @Override
            public void onResponse(String response) {
                dissDialog();
                ProofBean proofBean = FastOk.get(response, ProofBean.class);
                if(proofBean != null && "1".equals(proofBean.getStatus())){
                    //存在此用户名，跳转
                    Intent intent = new Intent(NewlyResetPassWordActivity.this,ResetPassWordActivity.class);
                    intent.putExtra("userName",et_name);
                    intent.putExtra("title","忘记密码");
                    startActivity(intent);
                    finish();
                }else {
                    MyToast.showToast(NewlyResetPassWordActivity.this,"用户名不存在");
                }
            }
        },pair);

    }
}
